package com.quanroon.ysq.queue.delay_queue;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import lombok.Data;

import java.util.Date;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author quanroong.ysq
 * @version 1.0.0
 * @description
 * @createtime 2021/9/8 20:56
 */
@Data
public class DelayInfo implements Delayed {

    private String orderId;
    private int delayTimeSecond;
    private DateTime expireTime;

    public DelayInfo(String orderId, int time){
        this.orderId = orderId;
        this.delayTimeSecond = time;
        this.expireTime = DateUtil.offsetSecond(new Date(), delayTimeSecond);
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(expireTime.getTime()-System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        return this.delayTimeSecond - ((DelayInfo)o).delayTimeSecond;
    }

    @Override
    public String toString() {
        return "DelayInfo{" +
                "orderId='" + orderId + '\'' +
                ", delayTimeSecond=" + delayTimeSecond +
                ", expireTime=" + expireTime +
                '}';
    }
}
